package com.leoman.post.controller;

import com.leoman.common.controller.common.GenericEntityController;
import com.leoman.common.factory.DataTableFactory;
import com.leoman.entity.Constant;
import com.leoman.exception.GeneralExceptionHandler;
import com.leoman.post.entity.PostComment;
import com.leoman.post.service.PostCommentService;
import com.leoman.post.service.impl.PostCommentServiceImpl;
import com.leoman.user.service.UserService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 *
 * Created by 蔡琦 on 2016/7/20 0020.
 */
@Controller
@RequestMapping(value = "admin/postComment")
public class PostCommentController extends GenericEntityController<PostComment, PostComment, PostCommentServiceImpl> {

    @Autowired
    private PostCommentService postCommentService;

    @Autowired
    private UserService userService;

    /**
     * 列表页面
     *
     * @return path
     */
    @RequestMapping(value = "/index")
    public String index() {
        return "post/upCommentList";
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/list")
    public void list(HttpServletResponse response,
                     String content,
                     String mobile,
                     String name,
                     Integer draw,
                     Integer start,
                     Integer length) {
        try {

            int pageNum = getPageNum(start, length);

            // 用户帖评论列表
            Page<PostComment> page = postCommentService.pageList(content, mobile, name, Constant.POST_TYPE_003, null, pageNum, length);
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, emptyData);
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    private Integer delete(Long commentId) {

        /*PostComment comment = postCommentService.queryByPK(commentId);
        postCommentService.delete(comment);*/
        postCommentService.deleteComment(commentId);

        return 1;
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteBatch(String ids) {
        Long[] arrayId = JsonUtil.json2Obj(ids, Long[].class);
        for (Long id : arrayId) {
            postCommentService.deleteComment(id);
        }
        return 1;
    }

    /**
     * 列表页面
     *
     * @return path
     */
    @RequestMapping(value = "/indexPost")
    public String indexPost() {
        return "post/gfCommentList";
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/postList")
    public void postList(HttpServletResponse response,
                     String name,
                     String mobile,
                     String content,
                     Integer draw,
                     Integer start,
                     Integer length) {
        try {

            int pageNum = getPageNum(start, length);

            // 官方帖评论列表
            Page<PostComment> page = postCommentService.page(name, mobile, content, null, null, pageNum, length);
            /*PostComment comment = null;
            for (PostComment postComment : page.getContent()) {
                comment = postCommentService.queryByPK(postComment.getToPostCommentId());
                postComment.setPostComment(comment.getContent());
            }*/
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, emptyData);
        }
    }

    /**
     * 回复
     */
    @RequestMapping(value = "/replay")
    @ResponseBody
    private Integer replay(Long commentId, Long toUserId, Long fromUserId, String content) {

        PostComment postComments = postCommentService.queryByPK(commentId);
        PostComment postComment = new PostComment();
        postComment.setPostBase(postComments.getPostBase());
        postComment.setFromUser(userService.queryByPK(fromUserId));
        postComment.setToUser(userService.queryByPK(toUserId));

        if (postComments.getType() == 0) {
            // 针对评论的回复
            postComment.setToPostCommentId(commentId);
        }else if (postComments.getType() == 1) {
            // 针对回复的回复
            postComment.setToPostCommentId(postComments.getToPostCommentId());
        }
        postComment.setContent(content);
        postComment.setStatus(Constant.COMMENT_TYPE_REPLY);
        postComment.setType(postComments.getType());
        postComment.setCreateDate(System.currentTimeMillis());
        postCommentService.save(postComment);

        return 1;
    }

}
